<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Facade\FlareClient\View;
use App\models\User1;
use App\models\User2;
use App\Http\Controllers\Controller;
use Symfony\Component\Console\Input\Input;
use Illuminate\Validation\Rule;

class TestController extends Controller
{
    //
    
    public function show($a){
        return $a;
    }

    public function select(){
        $list = DB::table('a')->get();
        return view('index2',compact('list'));
    }

    public function insert(){
        return DB::table('a')->insert([
            'id' => '2',
            'name' => '卢本伟',
            'age' => '17'
        ]);
    }

    public function update(){
        return DB::table('a')->where('id','1')->update(['name'=>'小刚']);
    }

    public function select1(){
       $user =  DB::table('a')->get();
       dd($user);
    }

    public function delete(){
       return DB::table('a')->where('id','1')->delete();
      
     }

    //调用模型
    public function model(){
        $model = new User1();
        $list = $model->Test();
        if($list > 0){
            return view('index3',compact('list'));
        }else{
            return '失败';
        }
    }
    //调用模型查找
    public function selectm(){
        $model = new User1();
        $list = $model->Test1();
        return view('index2',compact('list'));
    }

    public function updatem(){
        $model = new User1();
        $int = $model->Test2();
        return view('index4',compact('int'));
    }

    public function deletem(){
        $model = new User1();
        $int = $model->Test3();
        return view('index4',compact('int'));
    }


    //分页
    public function fenye(){
        $model = new User1();
        $list = $model->fenye();
        return view('1',compact('list'));
    }


    public function csrf(){
        return view('csrf');
    }

    public function test()
    {
        $control = DB::table('control')->get();
        return view('1',compact('control'));
    }

    public function store(Request $request){
    $validatedData = $request->validate([
        'title' => 'required|string',
        'body' => 'required|Integer',
    ]);


        // 验证通过，存储到数据库...
    }


    public function loginCreate(){
        return view('login');
    }
    
    public function loginPost(Request $request){
        $validatedData = $request->validate([
            'name' => 'bail|required|alpha',
            'password' => 'bail|required',
        ],[
            'name.required' => '用户名不能为空',
            'name.alpha' => '用户名必须是纯字符',
            'password.required' => '密码不能为空',
        ]);
      $name = $request->input('name');
      $password = $request->input('password');
      $user = User2::where(['username'=> $name,'password'=>$password])->first();
    //   $user = DB::table('users')->where(['username'=> $name,'password'=>$password])->first();
        if($user){
            session()->put("name",$name);
            session()->put("password",$password);
            $control = DB::table('control')->get();
            return view('shouye1',compact('control'));
        }else{
            // echo("<script>alert('账号或者密码错误')</script>");
            return redirect()->back()->withErrors('账号或者密码错误')->withInput();
        
        }
        
    }

    public function add(Request $request){
        return view('add');
    }

    public function AddJudge(Request $request){
        $validatedData = $request->validate([
            'title' => 'bail|required|unique:control,title',
            'control' => 'bail|required',
            'author'=> 'bail|required',
        ],[
            'title.unique' => '标题已存在',
            'title.required' => '用户名不能为空',
            'control.required' => '密码不能为空',
            'author.required' => '作者不能为空',
        ]);
        $title = $request->input('title');
      $article = $request->input('control');
      $author = $request->input('author');
      if ($request->session()->has('name')) {
        $add = DB::table('control')->insert([
            'title' => $title,
            'control' =>  $article,
            'author' => $author
        ]);
            $control = DB::table('control')->get();
            return view('shouye1',compact('control'));
    }else{
        return redirect('create/login')->withErrors('请登录')->withInput(); 
    }
     
    }
  
    public function UpdateJudge(Request $request){
        $validatedData = $request->validate([
            'title' => 'bail|required',
            'control' => 'bail|required',
            'author'=> 'bail|required',
        ],[
            'title.required' => '用户名不能为空',
            'control.required' => '密码不能为空',
            'author.required' => '作者不能为空',
        ]);
        $title = $request->input('title');
        $article = $request->input('control');
        $author = $request->input('author');
        $user = DB::table('control')->where(['title'=>$title])->first();
        if ($request->session()->has('name')) {
            if($user){
                DB::table('control')->where('title',$title)->update(['control'=>$article,'author'=>$author]);
                $control = DB::table('control')->get();
                return view('shouye1',compact('control'));
            }else{
                return view('updateError');
            }
        }else{
            return redirect('create/login')->withErrors('请登录')->withInput(); 
        }
       
    }

    public function SelectJudge(Request $request){
        $author = $request->input('author');
 
        $control = DB::table('control')->where(['author'=>$author])->get();
        if ($request->session()->has('name')) {
            if($control){
        
                return view('shouye1',compact('control'));
            }else{
                return view('updateError');
            }
        }else{
            return redirect('create/login')->withErrors('请登录')->withInput();
        }
    }

    public function DeleteJudge(Request $request){
        $validatedData = $request->validate([
            'title' => 'bail|required',
            'control' => 'bail|required',
            'author'=> 'bail|required',
        ],[
            'title.required' => '用户名不能为空',
            'control.required' => '密码不能为空',
            'author.required' => '作者不能为空',
        ]);
        $title = $request->input('title');
        $article = $request->input('control');
        $author = $request->input('author');
        $user = DB::table('control')->where(['title'=>$title,'control'=>$article,'author'=>$author])->first();
        if ($request->session()->has('name')) {
            if($user){
                DB::table('control')->where('title',$title)->delete();
                $control = DB::table('control')->get();
                return view('shouye1',compact('control'));
            }else{
                return view('updateError');
            }
        }else{
            return redirect('create/login')->withErrors('请登录')->withInput();
        }
    }

    public function exit(Request $request){
        $request->session()->flush();
        $control = DB::table('control')->get();
        return view('shouye1',compact('control'));
    }

    public function tiku(){
        $lib = DB::table('laravel')->get();
        return view('tiku',compact('lib'));
    }
}
